// Test1.cpp : Defines the entry point for the console application.
//
#include "SDL.h"

#include <windows.h>	
#include <GL/gl.h>
#include <GL/glu.h>

#include "GTestEngine.h"

#define TIMER_INTERVAL 30
//////////////////////////////////////////////////////////////////////////
CGEngine* engine = NULL;
//////////////////////////////////////////////////////////////////////////
static void setup_opengl( int width, int height )
{
	float ratio = (float) width / (float) height;

	
	/* Our shading model--Gouraud (smooth). */
	//glShadeModel( GL_SMOOTH );

	/* Culling. */
	//glCullFace( GL_BACK );
	//glFrontFace( GL_CCW );
	//glEnable( GL_CULL_FACE );

	/* Set the clear color. */
	//glClearColor( 0, 0, 0, 0 );

	/* Setup our viewport. */
	//glViewport( 0, 0, width, height );

	/*
	* Change to the projection matrix and set
	* our viewing volume.
	*/
	//glMatrixMode( GL_PROJECTION );
	//glLoadIdentity( );
	/*
	* EXERCISE:
	* Replace this with a call to glFrustum.
	*/
	//gluPerspective( 60.0, ratio, 1.0, 1024.0 );
	
}

Uint32 timer_callback(Uint32 interval)
{
	if(engine!=NULL)
		engine->OnTick(1);
	return interval;
}
int main( int argc, char* argv[] )
{
	if((SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)==-1)) { 
		printf("Could not initialize SDL: %s.\n", SDL_GetError());
		return -1;
	}
//////////////////////////////////////////////////////////////////////////
	/* Information about the current video settings. */
	const SDL_VideoInfo* info = NULL;
	/* Dimensions of our window. */
	int width = 0;
	int height = 0;
	/* Color depth in bits of our window. */
	int bpp = 0;
	/* Flags we will pass into SDL_SetVideoMode. */
	int flags = 0;

	//screen surface
	SDL_Surface *screen;

	// set this flag to 'true' to exit main application
	bool exitflag=false;

//////////////////////////////////////////////////////////////////////////

	info = SDL_GetVideoInfo( );

	width = 640;
	height = 480;
	bpp = info->vfmt->BitsPerPixel;

	SDL_SetTimer(TIMER_INTERVAL, timer_callback);

//////////////////////////////////////////////////////////////////////////
	// OpenGL Initialization
//////////////////////////////////////////////////////////////////////////

	// 5bits of red, green and blue, 16 bits of z-buffer 
	// and a double buffering feature
	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
	SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

	flags = SDL_OPENGL;
	
	screen = SDL_SetVideoMode( width, height, bpp, flags );

	if(screen==NULL)
	{
		fprintf( stderr, "Video mode set failed: %s\n",
			SDL_GetError( ) );
		SDL_Quit();
		return -1;
	}

	setup_opengl( width, height );

//////////////////////////////////////////////////////////////////////////

	engine = new CGTestEngine();
	engine->Init();
	SDL_Event event;
	while(exitflag==false)
	{
		while( SDL_PollEvent( &event ) )
		{
			/* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
			switch( event.type )
			{
			case SDL_KEYDOWN:
				engine->OnKeyDown(event.key);
				break;
			case SDL_KEYUP:
				if(event.key.keysym.sym==SDLK_ESCAPE)
					exitflag=true;
				else
					engine->OnKeyUp(event.key);
				break;
			case SDL_MOUSEBUTTONDOWN:
				switch(event.button.button)
				{
				case SDL_BUTTON_LEFT:
					engine->OnLMBDown(event.button);
				}
				break;
			case SDL_MOUSEBUTTONUP:
				switch(event.button.button)
				{
				case SDL_BUTTON_LEFT:
					engine->OnLMBUp(event.button);
				}
				break;
			case SDL_QUIT:
				exitflag = true;
				break;

			default:
				break;
			}
		}
		engine->OnIdle();
		SDL_Delay(10);
	}
	engine->Deinit();
	delete engine;


	SDL_Quit();

	printf("Quiting....\n");

	return 0;
}

